/*
 * $Id: XMLUtil.java 1096 2011-07-04 03:36:31Z jyang $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.core.common.util;

import java.util.List;

import javax.transaction.SystemException;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;

public final class XMLUtil {

    private XMLUtil() {
    }

    /**
     * Find a node's children, and the child's name and an attribute value have
     * given
     * 
     * @param parentEle
     * @param childEleName
     * @param attrName
     * @param attrValue
     * @return
     * @throws SystemException
     */
    public static Element searchChildByAttr(final Element parentEle, final String childEleName,
            final String attrName, final String attrValue) throws SystemException {
        List<Element> eleList = null;

        if (parentEle == null) {
            throw new SystemException("~~PARSE-XML-ERROR~~ parentEle is null !");
        }

        if (StringUtils.isBlank(childEleName)) {
            throw new SystemException("~~PARSE-XML-ERROR~~ childEleName is null !");
        }

        if (StringUtils.isBlank(attrName)) {
            throw new SystemException("~~PARSE-XML-ERROR~~ attrName is null !");
        }

        if (StringUtils.isBlank(attrValue)) {
            throw new SystemException("~~PARSE-XML-ERROR~~ attrValue is null !");
        }

        // The following is the exact search phrase used, akin
        // "attribute=value"
        eleList = parentEle.selectNodes(childEleName + "[@" + attrName + "='" + attrValue + "']");

        // The following phrase is used in fuzzy query, akin
        // "attribute = like 'value'"
        // eleList = parentEle.selectNodes("/[contains(@" + attrName + ",'"
        // + attrValue + "')]");

        if (eleList.size() == 0) {
            return null;
        }

        return eleList.get(0);
    }

    /**
     * Find a child node Txt value of node , and the child's name and an
     * attribute value have given
     * 
     * @param parentEle
     * @param childEleName
     * @param attrName
     * @param attrValue
     * @return
     * @throws SystemException
     */
    public static String getChildTxt(final Element parentEle, final String childEleName,
            final String attrName, final String attrValue) throws SystemException {

        Element child = searchChildByAttr(parentEle, childEleName, attrName, attrValue);
        if (child == null) {
            return null;
        }

        return child.getText();
    }
}
